/* Mohamed Zaid Bhura
 * CSE 2102
 * Lab 1
 * This is the DemoPanel class.
 */

import javax.swing.*;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseMotionAdapter;

class DemoPanel extends JPanel {
	private Color _color = Color.RED;
	private int squareX = 50;
	private int squareY = 50;
	private int squareW = 30;
	private int squareH = 30;
	
	public DemoPanel() {
		this.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		this.setBackground(Color.GREEN);
		
		 addMouseListener(new MouseAdapter() {
	            public void mousePressed(MouseEvent e) {
	                moveSquare(e.getX(),e.getY());
	            }
	        });
		 
		 addMouseMotionListener(new MouseAdapter() {
	            public void mouseDragged(MouseEvent e) {
	                moveSquare(e.getX(),e.getY());
	            }
	        });
	}
	
	public Dimension getPreferredSize() {
		return new Dimension(250, 200);
	}
	
	public void setColor(Color aColor){
		_color = aColor;
	}
	
    private void moveSquare(int x, int y) {
        int OFFSET = 1;
        if ((squareX!=x) || (squareY!=y)) {
            repaint(squareX,squareY,squareW+OFFSET,squareH+OFFSET);
            squareX=x;
            squareY=y;
            repaint(squareX,squareY,squareW+OFFSET,squareH+OFFSET);
        } 
    }
	
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.setColor(Color.BLUE);
		g.drawString("This panel is soooo cool!!!!!", 20, 40);
		g.setColor(_color);
        g.fillRect(squareX,squareY,squareW,squareH);
        g.setColor(Color.BLACK);
        g.drawRect(squareX,squareY,squareW,squareH);
	}
}